<%--
  Created by IntelliJ IDEA.
  User: xshuai
  Date: 2020/8/4/0004
  Time: 21:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Hello Spring Boot</title>
    <script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script type="text/javascript">
        function post(user) {
            let url = "./save"
            $.post({
                url: url,
                //此处需要告知传递参数类型为JSON,不能缺少
                contentType: "application/json",
                //将JSON转换为字符串传递
                data: JSON.stringify(user),
                //成功后的方法
                success: function (result, status) {
                    if (result == null || result.id == null) {
                        alert("插入失败");
                    }
                }
            });
        }

        for (let i = 0; i <= 10; i++) {
            let user = {
                'id': i,
                'username': 'user_name_' + i,
                'note': 'note_' + i,
                'roles': [{
                    'id': i,
                    'roleName': 'role_' + i,
                    'note': 'note_' + i
                }, {
                    'id': i + 1,
                    'roleName': 'role_' + (i + 1),
                    'note': 'note_' + (i + 1)
                }]
            };
            post(user)
        }
    </script>
</head>
<body>
<h1>操作MongoDB文档</h1>
</body>
</html>
